package config_manager

import (
	"github.com/golang/protobuf/proto"
	"go_server/game_server/dataconfig/configpb"
	"go_server/game_server/util"
	"go_server/zinx/zlog"
)

var (
	stcTableCurrency_SheetCurrency map[int32]*configpb.StcTableCurrency_SheetCurrency_7A6D5966
)

func load_StcTableCurrency_SheetCurrency(fileName string) {
	fullPath := configFilePath + fileName
	content := util.GetReadFileBuf(fullPath)

	zlog.Debug("load_StcTableCurrency_SheetCurrency start!!!")
	msg := &configpb.DataSet_StcTableCurrency_SheetCurrency_7A6D5966{}
	proto.Unmarshal(content, msg)

	stcTableCurrency_SheetCurrency = make(map[int32]*configpb.StcTableCurrency_SheetCurrency_7A6D5966)

	for _, info := range msg.Items {
		stcTableCurrency_SheetCurrency[info.Id] = info
	}
	zlog.Debug("load_StcTableCurrency_SheetCurrency complete!!!")
}

func GetConfigStcTableCurrency_SheetCurrency(id int32) (configpb.StcTableCurrency_SheetCurrency_7A6D5966, bool) {
	config, ok := stcTableCurrency_SheetCurrency[id]
	if !ok {
		zlog.Errorf("GetConfigStcTableCurrency_SheetCurrency Err!!! id: %d", id)
		return configpb.StcTableCurrency_SheetCurrency_7A6D5966{}, false
	}
	return configpb.StcTableCurrency_SheetCurrency_7A6D5966{
		Id:        config.Id,
		Buildid:   config.Buildid,
		Itemid:    config.Itemid,
		Transcur:  config.Transcur,
		Transdata: config.Transdata,
	}, true
}

func GetTableCurrencySheetCurrencyMap() map[int32]*configpb.StcTableCurrency_SheetCurrency_7A6D5966 {
	return stcTableCurrency_SheetCurrency
}

// 根据建筑id获取货币配置
func GetTableCurrencySheetCurrencyBYBuildId(buildId int32) *configpb.StcTableCurrency_SheetCurrency_7A6D5966 {
	currencyConfig := new(configpb.StcTableCurrency_SheetCurrency_7A6D5966)

	if stcTableCurrency_SheetCurrency != nil {
		for _, config := range stcTableCurrency_SheetCurrency {
			if buildId == config.Buildid { //根据建筑id获取货币配置
				currencyConfig.Id = config.Id
				currencyConfig.Itemid = config.Itemid
			}
		}
	}
	return currencyConfig
}

// 根据itemid查找货币id
func GetTableCurrencySheetCurrencyBYItemId() map[int32]int32 {
	allConfig := make(map[int32]int32)

	if stcTableCurrency_SheetCurrency != nil {
		for _, config := range stcTableCurrency_SheetCurrency {
			allConfig[config.Itemid] = config.Id
		}
	}

	return allConfig
}
